const data = { name: "zf", age: 20 };

function defineReactive(object, key) {
  let val = object[key];
  Object.defineProperty(object, key, {
    enumerable: true,
    configurable: true,
    get() {
      return val;
    },
    set(newVal) {
      console.log(`${key} 被修改为 ${newVal}`);
      val = newVal;
    },
  });
}
function defineReactive2(object, key) {
  let val = object[key];
  delete object[key];
  //   Object.assign(object, {
  //     get [key]() {
  //       return val;
  //     },
  //     set [key](newVal) {
  //       console.log(`${key} 被修改为 ${newVal}`);
  //       val = newVal;
  //     },
  //   });
}

defineReactive(data, "name");
defineReactive2(data, "age");
data.name = "xxx";
data.age = 32;
console.log(JSON.stringify(data));
